package com.huluobo.base.view;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public abstract class BaseActivity<V, P extends BasePresenter<V>> extends AppCompatActivity {
    protected P presenter;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayoutId());
        initView();
        initData();
        if (presenter != null) {
            presenter.attachView((V) this);
        }
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event == null || TextUtils.isEmpty(event.getTarget())) return;
        //判断是否为当前页面
        if (event.getTarget().equals(getClass().getSimpleName())) {
            handEvent(event);
        }
    }

    protected void handEvent(Event event) {

    }

    public void showProgressDialog(String message) {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(message);
        progressDialog.show();
    }

    public void dismissProgressDialog() {
        progressDialog.dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.disAttach();
        }
    }

    protected abstract void initData();

    protected abstract void initView();

    protected abstract int bindLayoutId();

}
